/*************************************************************************
	> File Name: system.c
	> Author:jason 
	> Mail:3297294149@qq.com 
	> Created Time: Sat 09 Dec 2023 02:00:07 PM CST
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
typedef struct user {
    char name[20];
    int age;
    char sex[10];
    int class;
    double height;
}user;
#define MAX_N 10000
user stu[MAX_N+5];
int stu_cnt;
const char * file_name="./data.txt";
typedef enum num_type
{
    LIST=1,
    ADD,
    MODIFY,
    DELETE,
    QUIT
}num_type;
int usage(){
    int num;
    do{
         printf("%d:list user info\n",LIST);
         printf("%d:add a new user\n",ADD);
         printf("%d:modify user info\n",MODIFY);
         printf("%d:delete user info\n",DELETE);
         printf("%d:quit\n",QUIT);
         printf("database:>>");
         scanf("%d",&num);
    }while(num<LIST || num>QUIT);
    return num;
}
int read_from_file(user *arr){
    int i=0;
    FILE *f=fopen(file_name,"r");
    if(f==NULL){printf("wrong");exit(1);}
    while(fscanf(f,"%s",arr[i].name)!=EOF){
        fscanf(f,"%d%s%d%lf",&arr[i].age,&arr[i].sex,&arr[i].class,&arr[i].height);
        i+=1;
    }
    fclose(f);
    return i;
}
void list_user()
{
    int len=0;
    len+=printf("%4s|%8s|%4s|%6s|%2s|%4s","id","name","age","sex","class","height");
    printf("\n");
    for(int i=0;i<len;i++){
        printf("=");
    }
    printf("\n");
    for(int i=0;i<stu_cnt;i++){
        printf("%4d|%8s|%4d|%6s|%2d|%4.2lf\n",i,stu[i].name,stu[i].age,stu[i].sex,stu[i].class,stu[i].height);
    }
}
void output_to_file(user *arr,int n){
    FILE *f=fopen(file_name,"a");
    for(int i=0;i<n;i++){
        fprintf(f,"%8s%4d%6s%2d%4.2lf\n",arr[i].name,arr[i].age,arr[i].sex,arr[i].class,arr[i].height);
    }
    fclose(f);
    return;
}
void add_user(){
    printf("add ner user:(name,age,sex,class,height)\n");
    printf("add:>>");
    scanf("%s%d%s%d%lf",stu[stu_cnt].name,&stu[stu_cnt].age,&stu[stu_cnt].sex,&stu[stu_cnt].class,&stu[stu_cnt].height);
    output_to_file(stu+stu_cnt,1);
    stu_cnt++;
    printf("add a user success\n");
    return 0;
}
void clear_file(){
    FILE *f=fopen(file_name,"w");
    fclose(f);
    return;
}
void restor_data_to_file(user *arr,int n){
    clear_file();
    output_to_file(arr,n);
}
void modify_user()
{
    list_user();
    int id;
    do{
        printf("modify id:");
        scanf("%d",&id);
    }while(id<0 || id>=stu_cnt);
    printf("add new user:(name,age,sex,class,height)\n");
    printf("modify:>>");
    scanf("%s%d%s%d%lf",stu[id].name,&stu[id].age,&stu[id].sex,&stu[id].class,&stu[id].height);
    restor_data_to_file(stu,stu_cnt);
    return ;

}
void delete_user()
{ 
    if(stu_cnt==0){printf("there is no stuent\n");return;}
    list_user();
    int id;
    do{
        printf("delete id:");
        scanf("%d",&id);
    }while(id<0 || id>=stu_cnt);
    printf("delete:>>\n");
    char s[100];
    printf("confirm(y/n):");
    getchar();
    //fflush(stdin);
    scanf("%[^\n]",s);
    if(s[0]!='y')return;
    for(int i=id+1;i<stu_cnt;i++){
        stu[i-1]=stu[i];
    }
    stu_cnt--;
    restor_data_to_file(stu,stu_cnt);
    return;
}
int main()
{
    stu_cnt=read_from_file(stu);
    int state=1;
    int num;
    while(state){
        num=usage();
        switch(num){
            case LIST:list_user();break;
            case ADD:add_user();break;
            case MODIFY:modify_user();break;
            case DELETE:delete_user();break;
            case QUIT:printf("bye\n");exit(0);
            default: printf("This doesn't exist");
        }
    }
    return 0;
}
